package com.yanqu.road.server.timer.banquet;

import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manager.banquet.CrossPartyMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.pb.banquet.CrossBanquetPb;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.List;

public class SyncBanquetTask extends GeneralTask {

    private boolean isRun = false;

    @Override
    public void doRun() {
        if(!isRun) {
            isRun = true;
            List<CrossBanquetData> banquetDataList = CrossBanquetMgr.getRunningBanquet();
            for(CrossBanquetData banquet : banquetDataList){
                syncToGameServer(banquet);
            }
            CrossPartyMgr.syncRelationshipToServer();
            isRun = false;
            CrossBanquetMgr.removeOverTimeBanquet();
        }
}

    private void syncToGameServer(CrossBanquetData banquet) {
        CrossBanquetProto.SyncCrossBanquetMsg.Builder respMsg = CrossBanquetPb.parseSyncCrossBanquetMsg(banquet);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_BANQUET_CROSS_SYNC_SYSTEM, respMsg);
        if("0".equals(banquet.getServerListStr())){
            int channelId = ConfigHelper.getInt("channelId");
            for(ServerInfo serverInfo :ServerListMgr.getOpenServerList(channelId)){
                MessageHelper.sendPacket(serverInfo.getServerId(), 0,pbMsg );
            }
        }else{
            for (long serverId : banquet.getServerList()) {
                MessageHelper.sendPacket(serverId, 0,pbMsg );
            }
        }
    }
}
